package com.hc.jdbc.base;
import java.sql.*;
import java.util.Properties;


import com.mysql.cj.jdbc.Driver;
public class Demo01 {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        //注册驱动
//        DriverManager.registerDriver(new Driver());
        //会注册两次，Driver

        //因此我们只需要触发静态代码块
        //利用反射触发
        Class.forName("com.mysql.cj.jdbc.Driver");

        //方法的重载：三个参数、一个参数、两个参数
        Connection connection = DriverManager.getConnection(
                "jdbc:mysql://127.0.0.1:3333/cd_db",
                "root", "123");

//        Connection connection = DriverManager.getConnection(
//                "jdbc:mysql://127.0.0.1:3333/cd_db?user=root&passsword=123");
//
//        Properties properties = new Properties();
//        properties.put("user","root");
//        properties.put("password","123");
//        Connection connection2 = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbctest", properties);


        //获取发送sql的对象
        Statement statement = connection.createStatement();
        //发送sql语句，并获取返回结果
        String sql = "select * from t_user";//查询语句，DQL语句
        ResultSet resultSet = statement.executeQuery(sql);
        //解析结果集
        while(resultSet.next()) {
//            int id = resultSet.getInt("id");
            String nickname = resultSet.getString("nickname");
            System.out.println(nickname);
        }

        //发送sql语句，并获取返回结果
        String sql2 = "insert into t_user values(3,'zhouwu','111','合理')";//查询语句，DQL语句
       int rows = statement.executeUpdate(sql2);
        System.out.println(rows);



        //资源关闭
        resultSet.close();
        statement.close();
        connection.close();//连接销毁关闭
    }
}